package com.biostime.qdingding.modules.qd.service;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.biostime.qdingding.common.persistence.Page;
import com.biostime.qdingding.common.persistence.TreeList;
import com.biostime.qdingding.common.service.TreeService;
import com.biostime.qdingding.modules.qd.dao.CenterDao;
import com.biostime.qdingding.modules.qd.entity.Center;
import com.biostime.qdingding.modules.qd.entity.Student;
import com.biostime.qdingding.modules.sys.entity.Area;
import com.biostime.qdingding.modules.sys.entity.User;
import com.google.common.collect.Maps;

/**
 * 早教中心Service
 * @author king
 * @version 2015-11-05
 */
@Service
@Transactional(readOnly = true)
public class CenterService extends TreeService<CenterDao, Center> {
	
	@Override
	public List<Center> findList(Center entity) {
		dataScopeFilter(entity, "dsf", "id=a.center_id", "id=a.create_by");
		
		return super.findList(entity);
	}
	
	public List<Center> getParentsTree() {
		List<Center> parents = dao.findList(new Center());
		TreeList<Center> treeList = new TreeList<Center>(parents,"├ ");
		return treeList.toList();
	}
	
	@Override
	public Page<Center> findPage(Page<Center> page, Center entity) {
		page.setOrderBy("a.LAYER,a.sort");
		entity.setPage(page);
		
		TreeList<Center> treeList = new TreeList<Center>(dao.findList(entity),"├ ");
		page.setList(treeList.toList());
		return page;
	}

	public List<Center> findListByUser(User curUser) {
		return findList(new Center());
	}

	public Map<String, String> findMapByUser(User user) {
		Map<String, String> map = Maps.newHashMap();
		List<Center> centers = dao.findListByUser(user);
		for(Center c : centers){
			if(!map.containsKey(c.getId())){
				map.put(c.getId(), "1");
			}
		}
		
		return map;
	}
	
	/* ==========================================================================
	 * 华丽分割线
	 * ==========================================================================*/
	public Center getCenterByArea(Area area){
		List<Center>centerList=dao.apiFindListByArea(area);
		if(centerList!=null && centerList.size()>0)return centerList.get(0);
		else return null;
	}
	public Center getCenterByStudent(Student student){
		List<Center>centerList=dao.apiFindListByStudent(student);
		if(centerList!=null && centerList.size()>0)return centerList.get(0);
		else return null;
	}
	public Center apiGetById(String centerId){
		return dao.apiGetById(centerId);
	}
	
	public List<Center> apiGetCenterList(){
		return dao.apiGetCenterList();
	}
}
